//670.最大交换
//https://leetcode.cn/problems/maximum-swap/?envType=daily-question&envId=2024-01-22
class Solution {
public:
    int maximumSwap(int num) {
        string s(to_string(num));

        int n = s.size();
        for(int i = 0;i<n;++i)
        {
            char maxn = s[i];
            for(int k = i;k<n;++k) if(s[k] > maxn) maxn = s[k];
            if(maxn <= s[i]) continue;
            int pos = n-1;
            while(pos > i && s[pos] != maxn) --pos;
            if(pos == i) continue;
            swap(s[i],s[pos]);
            return stoi(s);
        }

        return num;
    }
};